﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.iss.inventory.Entity;
using System.Data;
using System.Collections;
using com.iss.inventory.CustomModels;



namespace StationeryInv
{
    public partial class SupplierItemsforDO : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            com.iss.inventory.Entity.InvManagementEntities invent = new InvManagementEntities();
            if (Session["empID"] == null)
            {
                Response.Redirect("~/Presentation/login.aspx");
            }

            Label lbl = this.Master.FindControl("lblLogInName") as Label;
            if (lbl != null)
            {
                lbl.Text = Session["UserName"].ToString();
            }

            if (!IsPostBack)
            {
                string suppliername = Convert.ToString(Session["b"]);
                tbSupplierName.Text = suppliername;

                List<object> list = new List<object>();
                var t = from si in invent.StationeryItems
                        join suppl in invent.SupplierPriceLists on si.ItemID equals suppl.ItemID
                        join sup in invent.Suppliers on suppl.SupplierID equals sup.SupplierID
                        where sup.Name == tbSupplierName.Text && si.QtyLeft < si.ReorderLevel

                        select new { si.ItemID, si.Description, si.QtyLeft, si.ReorderLevel };
                list = t.ToList<object>();
                Gview.DataSource = list;

                Gview.DataBind();
            }




        }

        protected void btnSelectValues_Click(object sender, EventArgs e)
        {
            

            com.iss.inventory.Entity.InvManagementEntities invent = new InvManagementEntities();

            List<ShowPo> lisshow = new List<ShowPo>();
            foreach (GridViewRow gvrow in Gview.Rows)
            {
                ShowPo sh = new ShowPo();
                CheckBox chk = gvrow.FindControl("cbPO") as CheckBox;
                if (chk.Checked == true)
                {

                    sh.ItemID = int.Parse(gvrow.Cells[1].Text);
                    sh.Description = gvrow.Cells[2].Text;

                    int reorderqty1 = Convert.ToInt32(gvrow.Cells[1].Text);
                    int reorderqty = (int)(from si in invent.StationeryItems
                                           where si.ItemID == reorderqty1
                                           select si.ReorderQty).First();
                    sh.Qty = reorderqty;

                    double unitprice1 = Convert.ToInt32(gvrow.Cells[1].Text);

                    double unitprice = double.Parse((from spl in invent.SupplierPriceLists
                                                     where spl.ItemID == unitprice1 && spl.SupplierCategory == 1
                                                     select spl.SupplierPrice).First().ToString());

                    sh.Price = unitprice;
                    sh.Amount = reorderqty * unitprice;
                    lisshow.Add(sh);

                }



            }

            Session["SupplierItemsDO"] = lisshow;
            Response.Redirect("PurchaseOrders.aspx");



        }

       
    }
}